package admin.server.controller;


import admin.server.model.param.UmsRoleUpdateParam;
import admin.server.service.UmsRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import admin.common.vo.PageVo;
import admin.common.vo.ResponseVO;
import admin.server.model.dto.UmsRoleDetailDto;
import admin.server.model.dto.UmsRoleDto;
import admin.server.model.param.UmsRoleCreateParam;
import admin.dao.model.UmsRole;

import java.util.List;

/**
 * 后台用户角色管理
 * Created by zgb on 2018/9/30.
 */
@Controller
@Tag(name= "后台用户角色管理")
@RequestMapping("/role")
public class UmsRoleController {
    @Autowired
    private UmsRoleService roleService;

    @Operation(summary = "添加角色")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO create(@RequestBody UmsRoleCreateParam role) throws Exception {
        roleService.create(role);
        return ResponseVO.success();
    }

    @Operation(summary = "修改角色")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO update(@RequestBody UmsRoleUpdateParam role) throws Exception {
        roleService.update(role);
        return ResponseVO.success();
    }

    @Operation(summary = "批量删除角色")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO delete(@RequestParam("ids") List<Long> ids) {
        int count = roleService.delete(ids);
        if (count > 0) {
            return ResponseVO.success(count);
        }
        return ResponseVO.failed();
    }

    @Operation(summary = "获取所有角色")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsRole>> listAll() {
        List<UmsRole> roleList = roleService.list();
        return ResponseVO.success(roleList);
    }

    @Operation(summary = "根据角色名称分页获取角色列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<PageVo<UmsRoleDto>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageVo<UmsRoleDto> page = roleService.list(keyword, pageSize, pageNum);
        return ResponseVO.success(page);
    }

    @Operation(summary = "根据ID获取角色详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<UmsRoleDetailDto> detail(@PathVariable Long id) {
        UmsRoleDetailDto umsRole = roleService.detail(id);
        return ResponseVO.success(umsRole);
    }



}
